package com.xoa.util.cms.directive;

import com.xoa.dao.portalColumn.PortalColumnMapper;
import com.xoa.util.common.StringUtils;
import dm.jdbc.util.StringUtil;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;


/**
 * 站点root路径
 */
public class CMSRootPathDirrctive implements TemplateDirectiveModel {
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {

        String src = StringUtils.getString(map.get("src"));
        int siteId = StringUtils.getInteger(environment.getCustomAttribute("SITE_ID"),0);
        int curChannelId = StringUtils.getInteger(environment.getCustomAttribute("CHANNEL_ID"), 0);


        PortalColumnMapper portalColumnMapper = (PortalColumnMapper) environment.getCustomAttribute("portalColumnMapper");
        boolean preview = (Boolean) environment.getCustomAttribute("preview");
        Writer writer = environment.getOut();
        if(preview){
            writer.write("/cmsTmp/"+siteId+"/"+src);
        }else{
            String channelPath="";
            if(curChannelId>0){
                //资源路径赋值 给channelPaht 赋值 ,判断当前栏目是第几层，例如第三层 往上读三层 ../../../
                writer.write(channelPath+"/"+src);
            }else{
                writer.write(src);
            }
        }
    }
}
